<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- <script>
        function parseArrayString(arrayString) {
            let index = 0;

            function parse() {
                const result = [];
                let currentString = '';

                while (index < arrayString.length) {
                    const char = arrayString[index];

                    if (char === '[') {
                        // 遇到左括号，开始解析一个新的数组  
                        index++; // 跳过 '['  
                        result.push(parse()); // 递归调用解析子数组  
                    } else if (char === ']') {
                        // 遇到右括号，结束当前数组  
                        index++; // 跳过 ']'  
                        if (currentString) {
                            result.push(currentString.trim());
                            currentString = '';
                        }
                        return result; // 返回当前解析的数组  
                    } else if (char === ',') {
                        // 逗号分隔元素  
                        if (currentString) {
                            result.push(currentString.trim());
                            currentString = '';
                        }
                        index++; // 跳过 ','  
                    } else {
                        // 记录当前字符  
                        currentString += char;
                        index++;
                    }
                }

                // 处理最后的字符串（如果有的话）  
                if (currentString) {
                    result.push(currentString.trim());
                }

                return result; // 返回当前解析的数组  
            }

            return parse(); // 开始解析  
        }

        // 示例使用  
        const inputString = "[1, 2, [3, 4, [5, 6]], 7]";
        const result = parseArrayString(inputString);
        console.log(result);
    </script> -->
    <script>
        function parseArrayString(arrayString) {
            const stack = [];
            let currentArray = [];
            let currentString = '';

            for (let i = 0; i < arrayString.length; i++) {
                const char = arrayString[i];

                if (char === '[') {
                    // 遇到左括号，开始一个新的数组  
                    stack.push(currentArray);
                    currentArray = [];
                } else if (char === ']') {
                    // 遇到右括号，结束当前数组并返回上级数组  
                    if (currentString) {
                        currentArray.push(currentString.trim());
                        currentString = '';
                    }
                    const finishedArray = currentArray;
                    currentArray = stack.pop();
                    currentArray.push(finishedArray);
                } else if (char === ',') {
                    // 逗号表示一个元素的结束，存储当前字符串  
                    if (currentString) {
                        currentArray.push(currentString.trim());
                        currentString = '';
                    }
                } else {
                    // 记录当前字符  
                    currentString += char;
                }
            }

            // 处理最后的字符串（如果有的话）  
            if (currentString) {
                currentArray.push(currentString.trim());
            }

            return currentArray[0];
        }

        // 示例使用  
        const inputString = "[1, 2, [3, 4, [5, 6]], 7]";
        const result = parseArrayString(inputString);
        console.log(result);
    </script>
</body>

</html>